package com.njrsrc.android.tasks;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.njrsrc.android.bean.CompanyDetails;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 工作详情
 * 
 * @author Administrator
 * 
 */
public class CompanyDetailTask extends AsyncTask<Object, Integer, Void> {
	private String url;
	private Handler mHandler;
	private UserInfo userInfo;
	private CompanyDetails companyDetail;

	@Override
	protected Void doInBackground(Object... params) {
		if (params.length < 4) {
			return null;
		}
		mHandler = (Handler) params[0];
		url = (String) params[1];
		userInfo = (UserInfo) params[2];
		companyDetail = (CompanyDetails) params[3];
		JSONObject jsonObject = Helpers.getJSONArrayByGet(url);
		Message msg = mHandler.obtainMessage();
		try {
			if (jsonObject == null) {
				msg.what = MsgID.COMPANY_PROFILE_QUERY_ERROR;
			} else if (jsonObject.toString().contains("error_code")) {
				// 返回错误
				userInfo.setError_code(jsonObject.getString("error_code"));
				userInfo.setError_message(jsonObject.getString("error_message"));
				msg.what = MsgID.COMPANY_PROFILE_QUERY_FAIL;
			} else {
				msg.what = MsgID.COMPANY_PROFILE_QUERY_SUCCESS;
				companyDetail.setCompany_id(jsonObject.getString("company_id"));
				companyDetail.setName(jsonObject.getString("name"));
				companyDetail.setLogo_url(jsonObject.getString("logo_url"));
				companyDetail.setCover_url(jsonObject.getString("cover_url"));
//				 companyDetail.setLogo_url("http://imgstatic.baidu.com/img/image/shouye/yiping2.jpg");
//				 companyDetail.setCover_url("http://imgstatic.baidu.com/img/image/shouye/yiping1.jpg");
				companyDetail.setNature(jsonObject.getString("nature"));
				companyDetail.setIndustry(jsonObject.getString("industry"));
				companyDetail.setSize(jsonObject.getString("size"));
				companyDetail.setIntro(jsonObject.getString("intro"));
				companyDetail.setAddress(jsonObject.getString("address"));
				companyDetail.setContact(jsonObject.getString("contact"));
				companyDetail.setPhone(jsonObject.getString("phone"));
				companyDetail.setFax(jsonObject.getString("fax"));
				companyDetail.setEmail(jsonObject.getString("email"));
				companyDetail.setUrl(jsonObject.getString("url"));
				companyDetail.setIs_favorite(jsonObject.getString("is_favorite"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mHandler.sendMessage(msg);
		return null;
	}
}